package com.lcxw.hxzbadmin.controller.admincontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import com.sun.xml.internal.bind.v2.TODO;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbadmin.entity.pojo.AdminUser;
import com.lcxw.hxzbadmin.service.baseservice.AdminUserService;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbcommon.common.RedisUtils;
import com.lcxw.hxzbcommon.common.StringUtil;

@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminConfigController {
    private Logger logger = LoggerFactory.getLogger(AdminConfigController.class);

    @Resource
    AdminUserService adminUserService;

    @Resource
    RedisUtils redisUtils;

    @PostMapping("/password/reset")
    public Response resetAdminPassword(@RequestBody JSONObject requestParams){
        // TODO: 2020/7/4 body封装bean 
        String adminName = requestParams.getString("adminName");
        String oldPassword = requestParams.getString("oldPassword");
        String newPassword = requestParams.getString("newPassword");
        logger.info("admin {} reset password",adminName);

        try {
            AdminUser adminUser = adminUserService.queryAdminByName(adminName);
            if (!oldPassword.equals(adminUser.getPassword())){
                return Response.common(logger,"原密码错误。", ErrorCode.INVALID_PARAM,"wrong old passwod.",null);
            }
            adminUser.setPassword(newPassword);
            adminUserService.updateAdminByName(adminUser);

            String redisKey="admin:"+adminName+":token";
            redisUtils.string_delete(redisKey);
        } catch (Exception e) {
            return Response.dbFail(logger,"operate admin "+adminName);
        }

        return Response.common(logger,adminName+"reset password success.",0,"success",null);

    }

    @PostMapping("/add")
    public Response addAdmin(@RequestBody JSONObject requestParams){
        logger.info("admin {} add a new admin user.",requestParams.getString("adminName"));


        AdminUser adminUser = new AdminUser();
        adminUser.setAdminName(requestParams.getString("newAdminName"));
        adminUser.setPassword(requestParams.getString("password"));
        adminUser.setAddTime(StringUtil.getDateTime());

        try {
            adminUserService.insert(adminUser);
        } catch (Exception e) {
            return Response.dbFail(logger,"add a new admin user.");
        }

        return Response.ok();

    }

    @PostMapping("/delete")
    public Response deleteAdmin(@RequestBody JSONObject requestParams){
        String adminName = requestParams.getString("adminName");

        if (!"admin".equals(adminName)){
            return Response.common(logger,"delete admin fail,the user is not super admin.",ErrorCode.LOSE_EFFICACY,"no access,not super admin.",null);
        }

        int id = requestParams.getInteger("id");

        try {
            adminUserService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            return Response.dbFail(logger,"delete an admin user.");
        }

        return Response.ok();

    }


}
